package ar.edu.caece.compiler.grammars.sample2.factories;

import ar.edu.caece.compiler.grammars.sample2.grammar.E;
import ar.edu.caece.compiler.grammars.sample2.grammar.E1p;
import ar.edu.caece.compiler.grammars.sample2.tokens.SampleGrammarSymbol;
import ar.edu.caece.compiler.grammars.sample2.tokens.PlusToken;
import ar.edu.caece.compiler.grammars.sample2.tokens.SampleToken;
import ar.edu.caece.compiler.grammars.sample2.tokens.TokenVisitorInterface;
import ar.edu.caece.compiler.model.LexicalAnalyzer;

public class E1pFactory extends AbstractTokenVisitorFactory{

	private EFactory eFactory;
	private TokenVisitorInterface parseE;
	
	private static E1pFactory INSTANCE;
	
	private Initializer initializer = new Initializer() {

		@Override
		public void init() {
			initializer = NoOperationInitializer.getInstance();
			eFactory = EFactory.getInstance();
			parseE = eFactory.makeVisitor();
		}
		
	};
	
	public E1pFactory(LexicalAnalyzer analizadorLexico) {
		super(analizadorLexico);	
	}

	public E1pFactory() {
		// TODO Auto-generated constructor stub
	}

	//+ E
	@Override
	public TokenVisitorInterface makeChainedVisitor(final TokenVisitorInterface siguiente) {
		initializer.init();
		return new PlusToken.PlusChainVisitor(siguiente) {
			
			@Override
			public SampleGrammarSymbol plusCase(PlusToken llamador, SampleGrammarSymbol param) throws Exception {
				System.out.println("Generando E1p");
				return new E1p(llamador, (E) nextToken().execute(parseE, param));
			}
		};
	}

	@Override
	public TokenVisitorInterface makeVisitor() {
		initializer.init();
		return new PlusToken.PlusDefaultVisitor() {
			
			@Override
			public SampleGrammarSymbol plusCase(PlusToken llamador, SampleGrammarSymbol param) throws Exception {
				System.out.println("Generando E1p");
				return new E1p(llamador, (E) nextToken().execute(parseE, param));
			}
			
			@Override
			public SampleGrammarSymbol defaultCase(SampleToken llamador, SampleGrammarSymbol param) {
				throw new RuntimeException("Token inesperado");
			}
		};
	}

	public static E1pFactory getInstance() {
		if(INSTANCE==null) INSTANCE = new E1pFactory();
		return INSTANCE;
	}
}
